home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / UniqueKey / UniqueKey_oracle / MasterDetail.m < prev    next >
Encoding:
Text File  |  1995-02-17  |  7.7 KB  |  208 lines

  1. #import "UniqueKey.h"
  2. #import "MasterDetail.h"
  3.  
  4.  
  5. static BOOL _Debug = YES;
  6.  
  7. @implementation MasterDetail
  8.  
  9. /******************************************************************************
  10. * Create an instance of the UniqueKey object to grab blocks of <count> keys for
  11. * our use during insert.  The count is set low so you can see the SQL reserve
  12. * blocks of keys.  Up the count to something larger for it to be of use.
  13. *
  14. * The login information (connection dictionary) has been blanked out, since
  15. * you may elect to have the tables in some database other than PEOPLE, so we
  16. * elect to run a login panel.  Once the login is complete we can get the
  17. * connection dictionary from the adaptor and use that information to allow
  18. * UniqueKey to login and create its separate connection for key reservation.
  19. *
  20. * Many of the method calls in EOF return (id)<some protocol>, so you will notice
  21. * several casts to (id) to avoid warnings for nested method calls supported by
  22. * the returned object, but not by the protocol.  Alternatively, you can cast to
  23. * the exact class you know returned, such as (EODatabaseDataSource*) or
  24. * (EODetailDatabaseSource*).
  25. ******************************************************************************/
  26. - appDidInit:sender
  27. {
  28.     EOAdaptorChannel *eoAdaptorChannel = [[(id)[employeeController dataSource] 
  29.                                                         databaseChannel] adaptorChannel];
  30.     EOAdaptor        *eoAdaptor        = [[eoAdaptorChannel adaptorContext] adaptor];
  31.     
  32.     if(_Debug) [eoAdaptorChannel setDelegate:self];
  33.  
  34.     employeeEntity       = [[(id)[employeeController dataSource] entity] retain];
  35.     equipmentOwnerEntity = [[(id)[equipmentOwnerController dataSource] entity] retain];
  36.  
  37.     if(![eoAdaptor runLoginPanelAndValidateConnectionDictionary]) [NXApp terminate:self];
  38.     [UniqueKey setConnectionDictionary:[eoAdaptor connectionDictionary]];
  39.     
  40.     employeeUniqueKey = [[[UniqueKey alloc] initWithEntity:employeeEntity count:5] retain];
  41.     if(!employeeUniqueKey) [NXApp terminate:self];
  42.  
  43.     [self setFetchOrderFor:employeeController with:@"LastName" order:EOAscendingOrder];
  44.     [self setFetchOrderFor:equipmentOwnerController with:@"Description" order:EOAscendingOrder];
  45.  
  46.     [employeeController fetch:self];
  47.     return self;
  48. }
  49.  
  50.  
  51. /******************************************************************************
  52. * Set a controller's data source to fetch sorted by a given attribute name
  53. * and order.
  54. ******************************************************************************/
  55. - setFetchOrderFor:(EOController*)controller with:(NSString*)attributeName order:(EOOrdering)order
  56. {
  57.     id        dataSource = [controller dataSource];
  58.     id        attribute  = [[dataSource entity] attributeNamed:attributeName];
  59.     NSArray    *orderArray;
  60.     
  61.     orderArray = [NSArray arrayWithObject:
  62.         [EOAttributeOrdering attributeOrderingWithAttribute:attribute ordering:order]];
  63.     [dataSource setFetchOrder:orderArray];
  64.     return self;
  65. }
  66.  
  67.  
  68. /******************************************************************************
  69. * Generate unique keys for the new object.  Use the instance of UniqueKey
  70. * to dole out a key from its internal buffer.  The UniqueKey object has its
  71. * own channel to the DB which it uses to allocate blocks of keys.
  72. ******************************************************************************/
  73. - (BOOL)controller:(EOController *)controller willInsertObject:object atIndex: (unsigned)newIndex
  74. {
  75.     if(controller==employeeController)
  76.     {
  77.     NSNumber       *uniqueKey  = [NSNumber numberWithInt:[employeeUniqueKey nextKey]];
  78.     NSArray        *emptyArray = [[[NSArray alloc] init] autorelease];
  79.  
  80.     [object setObject:uniqueKey      forKey:@"EmpId"];
  81.     [object setObject:@"<LastName>"  forKey:@"LastName"];
  82.     [object setObject:@"<FirstName>" forKey:@"FirstName"];
  83.     [object setObject:@"<Phone>"     forKey:@"Phone"];
  84.     [object setObject:emptyArray     forKey:@"toEmpEquipment"];
  85.     return YES;
  86.     }
  87.     return NO;
  88. }
  89.  
  90.  
  91. /******************************************************************************
  92. * When an employee is deleted from the database, we need to remove that person's
  93. * ownership of their equipment.  We use the toEmpEquipment relationship to get
  94. * an NSArray of equipment objects and null the EmpId for each.
  95. ******************************************************************************/
  96. - (BOOL)controller:controller willDeleteObject:object
  97. {
  98.     NSArray         *eeArray;
  99.     NSEnumerator    *eeEnumerator;
  100.     EOGenericRecord    *equipment;
  101.  
  102.     if(controller==employeeController) {
  103.         eeArray      = [object objectForKey:@"toEmpEquipment"];
  104.         eeEnumerator = [eeArray objectEnumerator];
  105.         
  106.     while ((equipment = [eeEnumerator nextObject]) != nil)  {
  107.         [equipment setObject:[EONull null] forKey:@"EmpId"];
  108.         [(id)[employeeController dataSource] updateObject:equipment];
  109.     }
  110.     }
  111.     return YES;
  112. }
  113.  
  114.  
  115. /******************************************************************************
  116. * Bring up the assign equipment panel and start a modal session.  Construct 
  117. * an 'otherEquipment' qualifier to select equipment not currently assigned to
  118. * the selected employee.
  119. ******************************************************************************/
  120. - assignEquipmentToEmployee:sender
  121. {
  122.     EOGenericRecord    *employee;
  123.     NSNumber        *empId;
  124.     EOQualifier        *otherEquipment;
  125.     NSString        *qualifierString;
  126.     
  127.     employee = [(id)[equipmentForEmployeeController dataSource] masterObject];
  128.        empId    = [employee objectForKey:@"EmpId"];
  129.  
  130.     qualifierString = [NSString stringWithFormat:@"(EmpId != %@ or EmpId = NULL)",empId];
  131.     
  132.     otherEquipment = [[[EOQualifier alloc]
  133.          initWithEntity:equipmentOwnerEntity qualifierFormat:qualifierString] autorelease];
  134.     
  135.     [(id)[equipmentOwnerController dataSource] setQualifier:otherEquipment];
  136.     [equipmentOwnerController clearSelection];
  137.     [equipmentOwnerController fetch];
  138.     [[assignEquipmentPanel center] makeKeyAndOrderFront:nil];
  139.     [NXApp runModalFor:assignEquipmentPanel];
  140.     [assignEquipmentPanel orderOut:self];
  141.     [employeeController fetch];
  142.     return self;
  143. }
  144.  
  145. - assignEquipmentToEmployeeOK:sender
  146. {
  147.     EOGenericRecord    *employee;
  148.     NSNumber        *empId;
  149.     NSArray            *eoArray;
  150.     NSEnumerator    *eoEnumerator;
  151.     EOGenericRecord    *equipmentOwner;
  152.  
  153.     employee        = [(id)[equipmentForEmployeeController dataSource] masterObject];
  154.     empId            = [employee objectForKey:@"EmpId"];
  155.     eoArray            = [equipmentOwnerController selectedObjects];
  156.     eoEnumerator    = [eoArray objectEnumerator];
  157.  
  158.     while((equipmentOwner = [eoEnumerator nextObject]) != nil) {
  159.     [equipmentOwner setObject:empId forKey:@"EmpId"];
  160.     [(id)[equipmentOwnerController dataSource] updateObject:equipmentOwner];
  161.     }
  162.     [NXApp stopModal];
  163.     return self;
  164. }
  165.  
  166.  
  167. /******************************************************************************
  168. * Release the equipment shown in the detail view.  Null out the equipment owner's
  169. * EmpId for each.
  170. ******************************************************************************/
  171. - releaseEquipmentForEmployee:sender
  172. {
  173.     NSArray        *efeArray;
  174.     NSEnumerator    *efeEnumerator;
  175.     EOGenericRecord    *equipment;
  176.  
  177.     efeArray        = [equipmentForEmployeeController selectedObjects];
  178.     efeEnumerator    = [efeArray objectEnumerator];
  179.  
  180.     while( (equipment = [efeEnumerator nextObject]) != nil) {
  181.     [equipment setObject:[EONull null] forKey:@"EmpId"];
  182.     [(id)[equipmentForEmployeeController dataSource] updateObject:equipment];
  183.     }
  184.     [employeeController fetch];
  185.     return self;
  186. }
  187.  
  188.  
  189. /******************************************************************************
  190. * Echo SQL when debug is enabled.
  191. ******************************************************************************/
  192. - (EODelegateResponse)adaptorChannel:channel willEvaluateExpression:(NSMutableString *)expression
  193. {
  194.     NSLog(expression);
  195.     return EODelegateApproves;
  196. }
  197.  
  198. - (void)dealloc
  199. {
  200.     [employeeEntity release];
  201.     [equipmentOwnerEntity release];
  202.     [employeeUniqueKey release];
  203.     [super dealloc];
  204.    
  205. }
  206.  
  207. @end
  208.